package org.skydiveapphd;

import org.skydiveapplibrary.R;
import org.skydiveapplibrary.util.RigReminderManager;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MainActivity extends TabActivity
{
	// tab tags
	private static final String STATS_TAB = "STATISTICS";
	private static final String GEAR_TAB = "GEAR";
	private static final String MORE_TAB = "MORE";
	
	@Override 
	public void onCreate(Bundle savedInstanceState)
	{ 
		super.onCreate(savedInstanceState);
		
		// show main view
		setContentView(R.layout.main);
		
		// get tab host
		TabHost tabHost = getTabHost();
		tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
	
		//add stats tab
		View statsTabview = createTabView(tabHost.getContext(), getString(R.string.main_summary_tab_label), R.drawable.icon_summary);
		TabSpec statsTab = tabHost.newTabSpec(STATS_TAB);
		statsTab.setIndicator(statsTabview);
		statsTab.setContent(new Intent(this, SummaryActivity.class));
		tabHost.addTab(statsTab);
		
		// add gear tab
		View gearTabview = createTabView(tabHost.getContext(), getString(R.string.main_gear_tab_label), R.drawable.icon_gear);
		TabSpec gearTab = tabHost.newTabSpec(GEAR_TAB);
		gearTab.setIndicator(gearTabview);
		gearTab.setContent(new Intent(this, GearListActivity.class));
		tabHost.addTab(gearTab);

		// add more tab
		View moreTabview = createTabView(tabHost.getContext(), getString(R.string.main_more_tab_label), android.R.drawable.btn_star_big_off);
		TabSpec moreTab = tabHost.newTabSpec(MORE_TAB);
		moreTab.setIndicator(moreTabview);
		moreTab.setContent(new Intent(this, MoreActivity.class));
		tabHost.addTab(moreTab);
		
		// Create new fragment and transaction
		Fragment newFragment = new LogEntryListActivity();
		FragmentTransaction transaction = getFragmentManager().beginTransaction();
		// Replace whatever is in the fragment_container view with this fragment,
		// and add the transaction to the back stack
		transaction.replace(R.id.fragHolder, newFragment, Constants.TAG_LOG_ENTRY_LIST);

		// Commit the transaction
		transaction.commit();
		
		// check rig reminders
		RigReminderManager.getInstance().showRigReminderAlert(this);
	}
	
	private static View createTabView(final Context context, final String text, int imageID) {
	    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
	    ((TextView) view.findViewById(R.id.tabsText)).setText(text);
	    ((ImageView) view.findViewById(R.id.tabsImage)).setImageResource(imageID);
	    return view;
	}

	// Dirty but it works
	public void updateGearTab()
	{
		GearListActivity gla = (GearListActivity) getLocalActivityManager().getActivity(GEAR_TAB);
		gla.loadData();
	}
	
	// Dirty but it works
	public void updateStatsTab()
	{
		SummaryActivity sa = (SummaryActivity) getLocalActivityManager().getActivity(STATS_TAB);
		sa.loadData();
	}
	
	//Referenced in layouts to stop click on fragment being passed to objects underneath
	public void doNothing(View v) {}
}
